﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Kugar.Core.ExtMethod;

namespace Kugar.Core.WinForm.ControlFlash
{
    public static class DataGridViewControlFlashExtMethod
    {
        #region "DataGridView：开始闪烁"

        /// <summary>
        ///     闪烁整行
        /// </summary>
        /// <param name="dg">源网格控件</param>
        /// <param name="rowIndex">要闪烁的行号</param>
        /// <param name="flashColor">闪烁的颜色</param>
        public static void StartFlash(this DataGridView dg,int rowIndex,Color flashColor)
        {
            if (rowIndex<0 || rowIndex>dg.Rows.Count)
            {
                throw new ArgumentOutOfRangeException("rowIndex");
            }

            var cm=ControlFlashManage.Create();

            var flashItem = new DataGridViewFlashItem(dg, rowIndex, null);

            flashItem.FlashColor = flashColor;

            cm.ItemList.Add(flashItem);

            
        }

        /// <summary>
        ///     闪烁一个单元格
        /// </summary>
        /// <param name="dg">源网格控件</param>
        /// <param name="rowIndex">要闪烁的行号</param>
        /// <param name="columnIndex">要闪烁的列号</param>
        /// <param name="flashColor">闪烁的颜色</param>
        public static void StartFlash(this DataGridView dg, int rowIndex, int columnIndex, Color flashColor)
        {
            StartFlash(dg, rowIndex, dg.Columns[columnIndex], flashColor);
        }

        /// <summary>
        ///     闪烁一个单元格
        /// </summary>
        /// <param name="dg">源网格控件</param>
        /// <param name="rowIndex">要闪烁的行号</param>
        /// <param name="columnName">要闪烁的列名</param>
        /// <param name="flashColor">闪烁的颜色</param>
        public static void StartFlash(this DataGridView dg, int rowIndex, string columnName, Color flashColor)
        {
             StartFlash(dg, rowIndex, dg.Columns[columnName],flashColor);
        }

        /// <summary>
        ///     闪烁一个单元格
        /// </summary>
        /// <param name="dg">源网格控件</param>
        /// <param name="rowIndex">要闪烁的行号</param>
        /// <param name="column">要闪烁的列对象</param>
        /// <param name="flashColor">闪烁的颜色</param>
        public static void StartFlash(this DataGridView dg, int rowIndex, DataGridViewColumn column, Color flashColor)
        {
            if (rowIndex<0 || column==null)
            {
                throw new Exception("请指定正确的RowIndex和column参数");
            }

            var cm = ControlFlashManage.Create();

            var flashItem = new DataGridViewFlashItem(dg, rowIndex, column);

            flashItem.FlashColor = flashColor;

            cm.ItemList.Add(flashItem);
        }

        #endregion

        #region "DataGridView：暂停闪烁"

        /// <summary>
        ///     暂停闪烁整行，并将行设置为闪烁的颜色
        /// </summary>
        /// <param name="dg">源网格控件</param>
        /// <param name="rowIndex">要暂停闪烁的行号</param>
        public static void SuspendFlash(this DataGridView dg,int rowIndex)
        {
            if (rowIndex<=0)
            {
                throw new ArgumentOutOfRangeException("rowIndex");
            }

            var cm = ControlFlashManage.Create();

            for (int i = 0; i < cm.ItemList.Count; i++)
            {
                var item = (DataGridViewFlashItem)cm.ItemList[i];

                if (item==null)
                {
                    continue;
                }

                if (item.SourceControl==dg && item.RowIndex==rowIndex)
                {
                    item.Suspend();
                }
            }
        }

        /// <summary>
        ///     暂停闪烁一个单元格，并将行设置为闪烁的颜色
        /// </summary>
        /// <param name="dg">源网格控件</param>
        /// <param name="rowIndex">要暂停闪烁的行号</param>
        /// <param name="columnIndex">要暂停闪烁的列号</param>
        public static void SuspendFlash(this DataGridView dg, int rowIndex, int columnIndex)
        {
            SuspendFlash(dg, rowIndex, dg.Columns[columnIndex]);
        }

        /// <summary>
        ///     暂停闪烁一个单元格，并将行设置为闪烁的颜色
        /// </summary>
        /// <param name="dg">源网格控件</param>
        /// <param name="rowIndex">要暂停闪烁的行号</param>
        /// <param name="columnName">要暂停闪烁的列名</param>
        public static void SuspendFlash(this DataGridView dg, int rowIndex, string columnName)
        {
            SuspendFlash(dg, rowIndex, dg.Columns[columnName]);
        }

        /// <summary>
        ///     暂停闪烁一个单元格，并将行设置为闪烁的颜色
        /// </summary>
        /// <param name="dg">源网格控件</param>
        /// <param name="rowIndex">要暂停闪烁的行号</param>
        /// <param name="column">要暂停闪烁的列对象</param>
        public static void SuspendFlash(this DataGridView dg, int rowIndex, DataGridViewColumn column)
        {
            if (rowIndex <= 0)
            {
                throw new ArgumentOutOfRangeException("rowIndex");
            }

            if (column==null)
            {
                throw new ArgumentOutOfRangeException("column");
            }

            var cm = ControlFlashManage.Create();

            for (int i = 0; i < cm.ItemList.Count; i++)
            {
                var item = (DataGridViewFlashItem)cm.ItemList[i];

                if (item == null)
                {
                    continue;
                }

                if (item.SourceControl == dg && item.RowIndex == rowIndex && item.Column==column)
                {
                    item.Suspend();
                }
            }
        }

        #endregion

        #region "DataGridView：停止闪烁"

        public static void StopFlash(this DataGridView dg, int rowIndex)
        {
            if (rowIndex <= 0)
            {
                throw new ArgumentOutOfRangeException("rowIndex");
            }

            var cm = ControlFlashManage.Create();

            for (int i = 0; i < cm.ItemList.Count; i++)
            {
                var item = (DataGridViewFlashItem)cm.ItemList[i];

                if (item == null)
                {
                    continue;
                }

                if (item.SourceControl == dg && item.RowIndex == rowIndex)
                {
                    item.Stop();
                }
            }
        }

        public static void StopFlash(this DataGridView dg, int rowIndex, int columnIndex)
        {
            SuspendFlash(dg, rowIndex, dg.Columns[columnIndex]);
        }

        public static void StopFlash(this DataGridView dg, int rowIndex, string columnName)
        {
            SuspendFlash(dg, rowIndex, dg.Columns[columnName]);
        }

        public static void StopFlash(this DataGridView dg, int rowIndex, DataGridViewColumn column)
        {
            if (rowIndex <= 0)
            {
                throw new ArgumentOutOfRangeException("rowIndex");
            }

            if (column == null)
            {
                throw new ArgumentOutOfRangeException("column");
            }

            var cm = ControlFlashManage.Create();

            for (int i = 0; i < cm.ItemList.Count; i++)
            {
                var item = (DataGridViewFlashItem)cm.ItemList[i];

                if (item == null)
                {
                    continue;
                }

                if (item.SourceControl == dg && item.RowIndex == rowIndex && item.Column == column)
                {
                    item.Stop();
                }
            }
        }

        #endregion

        #region "DataGridView：删除闪烁"

        public static void RemoveFlash(this DataGridView dg)
        {
            if (dg==null)
            {
                return;
            }

            var cm = ControlFlashManage.Create();

            cm.ItemList.Remove((s) =>
                                   {
                                       if (!(s is DataGridViewFlashItem))
                                       {
                                           return false;
                                       }

                                       var item = (DataGridViewFlashItem) s;

                                       if (item.SourceControl != dg)
                                       {
                                           return false;
                                       }

                                       dg.Invoke(new MethodInvoker(item.Stop));

                                       return true;
                                   });
        }

        public static void RemoveFlash(this DataGridView dg, int rowIndex)
        {
            if (rowIndex <= 0)
            {
                throw new ArgumentOutOfRangeException("rowIndex");
            }



            var cm = ControlFlashManage.Create();

            cm.ItemList.Remove((s) =>
                                   {
                                       if (!(s is DataGridViewFlashItem))
                                       {
                                           return false;
                                       }

                                       var item = (DataGridViewFlashItem) s;

                                       if (item.SourceControl != dg || item.RowIndex != rowIndex)
                                       {
                                           return false;
                                       }

                                       dg.Invoke(new MethodInvoker(item.Stop));

                                       return true;
                                   });
            //var tempList = new List<DataGridViewFlashItem>();
            //for (int i = 0; i < cm.ItemList.Count; i++)
            //{
            //    var item = (DataGridViewFlashItem)cm.ItemList[i];

            //    if (item == null)
            //    {
            //        continue;
            //    }

            //    if (item.SourceControl == dg && item.RowIndex == rowIndex)
            //    {
            //        item.Stop();
            //        tempList.Add(item);
            //    }
            //}

            //if (tempList.Count>0)
            //{
            //    for (int i = 0; i < tempList.Count; i++)
            //    {
            //        cm.ItemList.Remove(tempList[i]);
            //    }
            //}
        }

        public static void RemoveFlash(this DataGridView dg, int rowIndex, int columnIndex)
        {
            SuspendFlash(dg, rowIndex, dg.Columns[columnIndex]);
        }

        public static void RemoveFlash(this DataGridView dg, int rowIndex, string columnName)
        {
            SuspendFlash(dg, rowIndex, dg.Columns[columnName]);
        }

        public static void RemoveFlash(this DataGridView dg, int rowIndex, DataGridViewColumn column)
        {
            if (rowIndex <= 0)
            {
                throw new ArgumentOutOfRangeException("rowIndex");
            }

            if (column == null)
            {
                throw new ArgumentOutOfRangeException("column");
            }

            var cm = ControlFlashManage.Create();

            DataGridViewFlashItem tempItem = null;

            for (int i = 0; i < cm.ItemList.Count; i++)
            {
                var item = (DataGridViewFlashItem)cm.ItemList[i];

                if (item == null)
                {
                    continue;
                }

                if (item.SourceControl == dg && item.RowIndex == rowIndex && item.Column == column)
                {
                    item.Stop();
                    break;
                }
            }

            cm.ItemList.Remove(tempItem);

        }

        #endregion
    }

    public static class GeneralControlFlashExtMethod
    {
        /// <summary>
        ///     开始闪烁一个控件
        /// </summary>
        /// <param name="control">源控件</param>
        /// <param name="flashColor">闪烁的颜色</param>
        public static void StartFlash(this Control control,Color flashColor)
        {
            if (control==null)
            {
                throw new ArgumentOutOfRangeException("control");
            }

            var cm = ControlFlashManage.Create();

            var flashItem = new GeneralControlFlashItem(control);

            flashItem.FlashColor = flashColor;

            cm.ItemList.Add(flashItem);

            //control.Invoke(new MethodInvoker(item.Suspend));
        }

        /// <summary>
        ///     暂停控件，并将行设置为闪烁的颜色
        /// </summary>
        /// <param name="control">源控件</param>
        public static void SuspendFlash(this Control control)
        {
            if (control == null)
            {
                throw new ArgumentOutOfRangeException("control");
            }

            var cm = ControlFlashManage.Create();

            for (int i = 0; i < cm.ItemList.Count; i++)
            {
                var item = (GeneralControlFlashItem)cm.ItemList[i];

                if (item == null)
                {
                    continue;
                }

                if (item.SourceControl == control)
                {
                    control.Invoke(new MethodInvoker(item.Suspend));
                    break;
                }
            }
        }

        /// <summary>
        ///     停止闪烁，并将行设置为原色
        /// </summary>
        /// <param name="control">源控件</param>
        public static void StopFlash(this Control control)
        {
            if (control == null)
            {
                throw new ArgumentOutOfRangeException("control");
            }

            var cm = ControlFlashManage.Create();

            for (int i = 0; i < cm.ItemList.Count; i++)
            {
                var item = (GeneralControlFlashItem)cm.ItemList[i];

                if (item == null)
                {
                    continue;
                }

                if (item.SourceControl == control)
                {
                    control.Invoke(new MethodInvoker(item.Stop));
                    break;
                }
            }
        }

        /// <summary>
        ///     删除控件闪烁
        /// </summary>
        /// <param name="control">源控件</param>
        public static void RemoveFlash(this Control control)
        {
            if (control == null)
            {
                throw new ArgumentOutOfRangeException("control");
            }

            var cm = ControlFlashManage.Create();

            GeneralControlFlashItem tempItem = null;

            for (int i = 0; i < cm.ItemList.Count; i++)
            {
                var item =cm.ItemList[i];

                if (item == null)
                {
                    continue;
                }

                if (item.SourceControl == control)
                {
                    control.Invoke(new MethodInvoker(item.Stop));
                    break;
                }
            }

            cm.ItemList.Remove(tempItem);

        }
    }
}
